package com.basic.library.binding.viewadapter.recyclerview;


import android.graphics.Color;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.basic.library.utils.ScreenUtils;
import com.basic.library.view.recyclerview.RecycleViewDivider;

/**
 * Created by goldze on 2017/6/16.
 */
public class LineManagers {
    protected LineManagers() {
    }

    public interface LineManagerFactory {
        RecyclerView.ItemDecoration create(RecyclerView recyclerView);
    }


    public static LineManagerFactory both() {
        return new LineManagerFactory() {
            @Override
            public RecyclerView.ItemDecoration create(RecyclerView recyclerView) {
                return new DividerLine(recyclerView.getContext(), DividerLine.LineDrawMode.BOTH);
            }
        };
    }

    public static LineManagerFactory horizontal() {
        return new LineManagerFactory() {
            @Override
            public RecyclerView.ItemDecoration create(RecyclerView recyclerView) {
                return new DividerLine(recyclerView.getContext(), DividerLine.LineDrawMode.HORIZONTAL);
            }
        };
    }

    public static LineManagerFactory horizontalTransparent(float dividerHeightDP, boolean topLine, boolean endLine) {
        return new LineManagerFactory() {
            @Override
            public RecyclerView.ItemDecoration create(RecyclerView recyclerView) {
                return new RecycleViewDivider(recyclerView.getContext()
                        , RecycleViewDivider.HORIZONTAL
                        , dividerHeightDP
                        , Color.TRANSPARENT).setTopLine(topLine).setEndLineSpace(endLine);
            }
        };
    }

    public static LineManagerFactory horizontal(float dividerHeightDP, int lineColor) {
        return new LineManagerFactory() {
            @Override
            public RecyclerView.ItemDecoration create(RecyclerView recyclerView) {
                return new RecycleViewDivider(recyclerView.getContext()
                        , RecycleViewDivider.HORIZONTAL
                        , dividerHeightDP
                        ,
                        lineColor == 0
                                ?
                                recyclerView.getContext()
                                        .obtainStyledAttributes(new int[]{android.R.attr.listDivider})
                                        .getColor(0, 0)
                                :
                                ContextCompat.getColor(recyclerView.getContext(), lineColor));
            }
        };
    }

    public static LineManagerFactory horizontal(float dividerHeightDP, int lineColor, int left, int right) {
        return new LineManagerFactory() {
            @Override
            public RecyclerView.ItemDecoration create(RecyclerView recyclerView) {
                RecycleViewDivider divider = new RecycleViewDivider(recyclerView.getContext()
                        , RecycleViewDivider.HORIZONTAL
                        , dividerHeightDP == 0 ? 1 : dividerHeightDP
                        , lineColor == 0 ?
                        recyclerView.getContext()
                                .obtainStyledAttributes(new int[]{android.R.attr.listDivider})
                                .getColor(0, 0)
                        : ContextCompat.getColor(recyclerView.getContext(), lineColor));
                divider.setLeftPadding(ScreenUtils.dip2px(recyclerView.getContext(), left));
                divider.setRightPadding(ScreenUtils.dip2px(recyclerView.getContext(), right));
                return divider;
            }
        };
    }

    public static LineManagerFactory horizontal(float dividerHeightDP, int lineColor, int left, int right, boolean topLine, boolean endLine) {
        return new LineManagerFactory() {
            @Override
            public RecyclerView.ItemDecoration create(RecyclerView recyclerView) {
                RecycleViewDivider divider = new RecycleViewDivider(recyclerView.getContext()
                        , RecycleViewDivider.HORIZONTAL
                        , dividerHeightDP == 0 ? 1 : dividerHeightDP
                        , lineColor == 0 ?
                        recyclerView.getContext()
                                .obtainStyledAttributes(new int[]{android.R.attr.listDivider})
                                .getColor(0, 0)
                        : ContextCompat.getColor(recyclerView.getContext(), lineColor));
                divider.setLeftPadding(ScreenUtils.dip2px(recyclerView.getContext(), left));
                divider.setRightPadding(ScreenUtils.dip2px(recyclerView.getContext(), right));
                return divider.setTopLine(topLine).setEndLineSpace(endLine);
            }
        };
    }

    public static LineManagerFactory vertical() {
        return new LineManagerFactory() {
            @Override
            public RecyclerView.ItemDecoration create(RecyclerView recyclerView) {
                return new DividerLine(recyclerView.getContext(), DividerLine.LineDrawMode.VERTICAL);
            }
        };
    }

    public static LineManagerFactory grid(int dividerHeightDP) {
        return new LineManagerFactory() {
            @Override
            public RecyclerView.ItemDecoration create(RecyclerView recyclerView) {
                return new RecycleViewDivider(recyclerView.getContext()
                        , RecycleViewDivider.GRID
                        , dividerHeightDP
                        , Color.TRANSPARENT);
            }
        };
    }

    public static LineManagerFactory grid(int dividerHeightDP, boolean topLine) {
        return new LineManagerFactory() {
            @Override
            public RecyclerView.ItemDecoration create(RecyclerView recyclerView) {
                return new RecycleViewDivider(recyclerView.getContext()
                        , RecycleViewDivider.GRID
                        , dividerHeightDP
                        , Color.TRANSPARENT)
                        .setTopLine(topLine);
            }
        };
    }

    public static LineManagerFactory gridIncludeEdge(int dividerHeightDP, boolean topLine) {
        return new LineManagerFactory() {
            @Override
            public RecyclerView.ItemDecoration create(RecyclerView recyclerView) {
                return new RecycleViewDivider(recyclerView.getContext()
                        , RecycleViewDivider.GRID
                        , dividerHeightDP
                        , Color.TRANSPARENT)
                        .setTopLine(topLine)
                        .setIncludeEdge(true);
            }
        };
    }
}
